import { createRouter, createWebHistory,RouteRecordRaw} from "vue-router"
import { createVNode,render } from 'vue';
import lodingBar from "@/components/lodingBar.vue";
const Vnode= createVNode(lodingBar)
render(Vnode,document.body);
//创建虚拟dom挂载到render渲染函数上
const routes: Array<RouteRecordRaw> = [

    {
        path: '/index',
        alias: '/',
        name: 'Layout',
        component:()=>import('@/layout/index.vue'),
    },
    {   
        path: '/login',
       
        name: 'Login',
        component: () => import('@/App.vue'),

    },
    {   
        path: '/register',
       
        name: 'Register',
        component: () => import('../pages/Register.vue'),

    },
    {
        path: '/helloword',
        name: 'HelloWord',
        component:()=>import('../components/HelloWorld.vue'),
    },{
        path: '/index',
        alias: '/',
        name: 'Layout',
        component:()=>import('@/layout/index.vue'),
    }

]
const whiteList = ['/login']
const router = createRouter({
    history:createWebHistory(),
    //路由滚动行为
    scrollBehavior:(to,from,savaPosition)=>{
        console.log(savaPosition);
        if(savaPosition){
            return savaPosition
        }else{
            top:0
        }
    },
    routes
})
//路由前置守卫
router.beforeEach((to,from,next)=>{
    Vnode.component?.exposed?.startLoding()
    if(whiteList.includes(to.path)||localStorage.getItem('token')){
    next()
    }else{
        next('/login')
    }
})
//路由后置守卫
router.afterEach(()=>{
    Vnode.component?.exposed?.endLoding() 
})
export default router